package com.woniu.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JwtUtil {
    //自定义秘钥
    private final static String  adminSecret = "ksaasfdasdfadsfwfrasdfsadffcasijfasadfasfdlkfjlasjflksajfd";
    private final static String  venueAdminSecret = "ksaasfdasdfadsfcasijfasadfasfdlkfjlasjflksajfd";
    private final static String userSecret = "asfasfffffffffuiahsudasdbqwibczxmxzcoiisbeuefbciasasf";
    private final static String coachSecret = "asfasfffffffffuiahsudsssasdbqwibczxmxzcoiisbeuefbciasasf";

    /**
     * 生成JWT Token
     *
     * @param body             要包含在Token中的用户信息
     * @param expiresInMinutes Token的有效时长（分钟）
     * @return 生成的Token字符串
     */
    public static String createAdminToken(Map<String, Object> body, Integer expiresInMinutes) {
        Key key = Keys.hmacShaKeyFor(adminSecret.getBytes());
        Date expirationDate = new Date(System.currentTimeMillis() + expiresInMinutes * 60 * 1000);
        return Jwts.builder().setClaims(body).setExpiration(expirationDate).signWith(key).compact();
    }

    public static String createVenueAdminToken(Map<String, Object> body, Integer expiresInMinutes) {
        Key key = Keys.hmacShaKeyFor(venueAdminSecret.getBytes());
        Date expirationDate = new Date(System.currentTimeMillis() + expiresInMinutes * 60 * 1000);
        return Jwts.builder().setClaims(body).setExpiration(expirationDate).signWith(key).compact();
    }


    /**
     * 生成JWT Token
     *
     * @param body             要包含在Token中的用户信息
     * @param expiresInMinutes Token的有效时长（分钟）
     * @return 生成的Token字符串
     */
    public static String createUserToken(Map<String, Object> body, Integer expiresInMinutes) {
        Key key = Keys.hmacShaKeyFor(userSecret.getBytes());
        Date expirationDate = new Date(System.currentTimeMillis() + expiresInMinutes * 60 * 1000);
        return Jwts.builder().setClaims(body).setExpiration(expirationDate).signWith(key).compact();
    }

    public static String createCoachToken(Map<String, Object> body, Integer expiresInMinutes) {
        Key key = Keys.hmacShaKeyFor(coachSecret.getBytes());
        Date expirationDate = new Date(System.currentTimeMillis() + expiresInMinutes * 60 * 1000);
        return Jwts.builder().setClaims(body).setExpiration(expirationDate).signWith(key).compact();
    }

    /**
     * 解析JWT Token
     *
     * @param token 要解析的Token字符串
     * @return 解析出的Token的Claims对象，包含了用户信息等内容
     */
    public static Claims parseAdminToken(String token) {
        Key key = Keys.hmacShaKeyFor(adminSecret.getBytes());
        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
    }

    public static Claims parseUserToken(String token) {
        Key key = Keys.hmacShaKeyFor(userSecret.getBytes());
        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
    }
    public static Claims parseVenueAdminToken(String token) {
        Key key = Keys.hmacShaKeyFor(venueAdminSecret.getBytes());
        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
    }

    public static Claims parseCoachToken(String token) {
        Key key = Keys.hmacShaKeyFor(coachSecret.getBytes());
        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
    }

    /**
     * 获取用户的ID
     *
     * @param token 要解析的Token字符串
     * @return 解析出用户的ID
     */
    public static Object getAdmin(String token) throws Exception {
        return parseAdminToken(token).get("admin");
    }


    public static Object getVenueAdmin(String token) throws Exception {
        return parseVenueAdminToken(token).get("venueAdmin");
    }

    public static Object getUser(String token, String userName) {
        return parseUserToken(token).get("user");
    }

    public static Object getCoach(String token) throws Exception {
        return parseCoachToken(token).get("coach");
    }

}
